package cn.edu.laud.sany.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MyErrorDataLog
{
	private FileWriter fWriter;
	
	public static MyErrorDataLog getMyErrorDataLog(String fileName)
	{
		MyErrorDataLog log = new MyErrorDataLog();
		File file = new File(fileName);
		if (file.exists() == false)
		{
			try
			{
				file.createNewFile();
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
		}
		try
		{
			log.fWriter = new FileWriter(file, true);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return null;
		}
		return log;
	}
	
	public synchronized void printLog(String fileName, long lineNum, String data)
	{
		try
		{
			StringBuilder sb = new StringBuilder();
			sb.append(fileName);
			sb.append("\tLine");
			sb.append(lineNum);
			sb.append("\t");
			sb.append(data);
			sb.append("\r\n");
			String lineString = sb.toString();
			fWriter.write(lineString);
			fWriter.flush();
			System.out.print("Error Format Data: " + lineString);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public synchronized void printTooManyDataLog(String fileName, long lineNum,
			String data)
	{
		try
		{
			StringBuilder sb = new StringBuilder();
			sb.append(fileName);
			sb.append("\tLine");
			sb.append(lineNum);
			sb.append("\t");
			sb.append(data);
			sb.append(" [Serial Number exceed 100, will lost Data!]");
			sb.append("\r\n");
			String lineString = sb.toString();
			fWriter.write(lineString);
			fWriter.flush();
			System.out.print("Serial Number Error: " + lineString);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
}
